import 'dart:convert';

import 'package:latlong2/latlong.dart';
import 'package:spring_fairy/util/amap_api.dart';
class PointRecord {
  double latitude;
  double longitude;
  String? address;
  String? weather;
  double? temperature;

  PointRecord({
    required this.latitude,
    required this.longitude,
     });

  static Future<PointRecord> searchAddress(LatLng latlng) async {
    var addressMap = await getAddressByLatlng(latlng);
    PointRecord pointRecord = PointRecord(latitude: latlng.latitude,
        longitude: latlng.longitude);


    if(addressMap == null) {
      return pointRecord;
    }
    String address = addressMap['regeocode']['formatted_address'];
    pointRecord.address = address;

    var weatherMap = await getWeather(latlng);
    if(weatherMap == null) {
      return pointRecord;
    }

    DateTime now = DateTime.now();
    bool isDayWeather = now.hour >= 6 && now.hour <= 18;

    late String weather;
    double? temp;
    if(isDayWeather) {
      weather = weatherMap['forecasts'][0]['casts'][0]['dayweather'];
      temp = double.tryParse(weatherMap['forecasts'][0]['casts'][0]['daytemp'].toString());
    } else {
      weather = weatherMap['forecasts'][0]['casts'][0]['nightweather'];
      temp = double.tryParse(weatherMap['forecasts'][0]['casts'][0]['nighttemp'].toString());
    }

    pointRecord.weather = weather;
    pointRecord.temperature = temp;
    return pointRecord;
  }


  @override
  String toString() {
    return "position:${latitude},${longitude}  address:${address},weather:${weather},temp:${temperature}";
  }
}